群内照片遍历,加载更多

chengzhenyu 8 years ago
parent
commit
2ca61fe2f6

+ 127 - 21
app/src/main/java/ai/pai/client/activity/PhotoDetailsActivity.java

@@ -65,6 +65,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
65 65
     private ImageView groupAvatarImg;
66 66
     private TextView groupNameText;
67 67
     private ViewPager photoPager;
68
+    private DetailPhotoPageAdapter photoPageAdapter;
68 69
     private ImageView uploaderAvatarImg;
69 70
     private TextView uploaderNameText;
70 71
     private TextView uploadTimeText;
@@ -102,7 +103,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
102 103
     private HttpPostTask getPriceTask;
103 104
     private HttpPostTask checkOrderTask;
104 105
     private HttpPostTask getPhotoOrderInfoTask;
105
-
106
+    private HttpPostTask fetchPhotosTask;
106 107
     private ArrayList<GroupPhotoItem> photoList;
107 108
     private GroupPhotoItem currentPhotoItem;
108 109
 
@@ -119,7 +120,8 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
119 120
     private static final String PAY_TYPE_ORIGIN = "origin";
120 121
 
121 122
     private static final String APP_ID = "wx4e22a0c8ae6d766d";
122
-
123
+    
124
+    private int currentGroupPhotoId;
123 125
 
124 126
     @Override
125 127
     protected void onCreate(Bundle savedInstanceState) {
@@ -135,12 +137,18 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
135 137
             photoList = (ArrayList<GroupPhotoItem>)getIntent().getSerializableExtra("list");
136 138
         }
137 139
         if(photoList==null){
138
-            photoList = new ArrayList<GroupPhotoItem>();
140
+            photoList = new ArrayList<>();
139 141
             photoList.add(currentPhotoItem);
140 142
         }
141 143
         fromGroupPage = getIntent().getBooleanExtra("fromGroup",false);
144
+        if(fromGroupPage){
145
+            currentGroupPhotoId=  DBService.getInstance(this).getCurrentIdByGroupId(currentPhotoItem.groupId);
146
+        }
147
+        if(photoList.size()>=300){
148
+            currentGroupPhotoId = 0;
149
+        }
142 150
         initViews();
143
-        DetailPhotoPageAdapter photoPageAdapter = new DetailPhotoPageAdapter(this,photoList);
151
+        photoPageAdapter = new DetailPhotoPageAdapter(this,photoList);
144 152
         photoPager.setAdapter(photoPageAdapter);
145 153
         photoPager.addOnPageChangeListener(this);
146 154
         photoPager.setCurrentItem(photoList.indexOf(currentPhotoItem));
@@ -157,6 +165,10 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
157 165
             PayListenerManager.getInstance().removePayResultListener("play");
158 166
             isPayListenerRegistered = false;
159 167
         }
168
+        if(fetchPhotosTask!=null){
169
+            fetchPhotosTask.cancel(true);
170
+            fetchPhotosTask = null;
171
+        }
160 172
         if (fetchThumbUpListTask != null) {
161 173
             fetchThumbUpListTask.cancel(true);
162 174
             fetchThumbUpListTask = null;
@@ -415,9 +427,14 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
415 427
         doWXPayTask(fee,type);
416 428
     }
417 429
 
430
+
418 431
     @Override
419 432
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
420
-
433
+        if(position==photoList.size()-1){
434
+            if(fetchPhotosTask!=null && fetchPhotosTask.getStatus()== AsyncTask.Status.RUNNING){
435
+                Toast.makeText(this,R.string.please_wait,Toast.LENGTH_SHORT).show();
436
+            }
437
+        }
421 438
     }
422 439
 
423 440
     @Override
@@ -425,6 +442,15 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
425 442
         currentPhotoItem = photoList.get(position);
426 443
         updateDetailHead();
427 444
         updateDetailBottom();
445
+        if(!fromGroupPage){
446
+            return;
447
+        }
448
+        if(position>=photoList.size()-1){
449
+            Bundle bundle = new Bundle();
450
+            bundle.putString("group_id",currentPhotoItem.groupId);
451
+            bundle.putString("current_id",String.valueOf(currentGroupPhotoId));
452
+            loadMoreGroupPhotos(bundle);
453
+        }
428 454
     }
429 455
 
430 456
     @Override
@@ -432,6 +458,87 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
432 458
 
433 459
     }
434 460
 
461
+    private synchronized void loadMoreGroupPhotos(final Bundle bundle){
462
+        if(fetchPhotosTask!=null && fetchPhotosTask.getStatus()== AsyncTask.Status.RUNNING){
463
+            return;
464
+        }
465
+        HashMap<String,String> params = new HashMap<>();
466
+        params.put("group_id",bundle.getString("group_id"));
467
+        params.put("user_id",Preferences.getInstance(this).getUserId());
468
+        params.put("current_id",bundle.getString("current_id"));
469
+        LogHelper.d(TAG,"loadMoreGroupPhotos with local currentId = "+bundle.getString("current_id"));
470
+
471
+        fetchPhotosTask = new HttpPostTask(this,params) {
472
+            ArrayList<GroupPhotoItem>  photoItems;
473
+            String  groupId = bundle.getString("group_id");
474
+            @Override
475
+            protected boolean parseResponse(Context context, String response) {
476
+                try{
477
+                    JSONObject json = new JSONObject(response);
478
+                    int status = json.getInt("status");
479
+                    if(status == 200){
480
+                        LogHelper.d(TAG,"group service loadMoreGroupPhotos  parseResponse status 200");
481
+                        JSONObject info = json.getJSONObject("data");
482
+                        if(currentGroupPhotoId==info.getInt("current_id")){
483
+                            return false;
484
+                        }
485
+                        JSONArray sessionArray = info.getJSONArray("photos");
486
+                        if(sessionArray!=null && sessionArray.length()>0) {
487
+                            photoItems = new ArrayList<>();
488
+                            for(int k = 0; k<sessionArray.length();k++){
489
+                                JSONObject sessionObj = sessionArray.getJSONObject(k);
490
+                                String sessionId = sessionObj.getString("session_id");
491
+                                JSONArray photoArray = sessionObj.getJSONArray("photos");
492
+                                if(photoArray!=null && photoArray.length()>0){
493
+                                    for(int m = 0; m<photoArray.length();m++){
494
+                                        JSONObject photoObj = photoArray.getJSONObject(m);
495
+                                        GroupPhotoItem groupPhotoItem = GroupPhotoItem.parseFromJSON(photoObj);
496
+                                        groupPhotoItem.sessionId = sessionId;
497
+                                        photoItems.add(groupPhotoItem);
498
+                                    }
499
+                                }
500
+                            }
501
+                            LogHelper.d(TAG,"group service loadMoreGroupPhotos  get new photos ,count="+photoItems.size());
502
+                        }
503
+
504
+                        currentGroupPhotoId = info.getInt("current_id");
505
+                        LogHelper.d(TAG,"loadMoreGroupPhotos with server currentId = "+currentGroupPhotoId+"groupId="+groupId);
506
+                        return true;
507
+                    }
508
+                }catch (Exception e){
509
+                    LogHelper.e(TAG,"group service loadMoreGroupPhotos error happen "+e);
510
+                }
511
+                return false;
512
+            }
513
+
514
+            @Override
515
+            protected void onPostFail(Context context) {
516
+                super.onPostFail(context);
517
+              
518
+            }
519
+
520
+            @Override
521
+            protected void onPostSuccess(Context context) {
522
+                super.onPostSuccess(context);
523
+
524
+                if(photoItems!=null && photoItems.size()>0){
525
+                    ArrayList<GroupPhotoItem> newPhotos = new ArrayList<>();
526
+                    for(GroupPhotoItem photoItem : photoItems){
527
+                        if(!photoList.contains(photoItem)){
528
+                            newPhotos.add(photoItem);
529
+                        }
530
+                    }
531
+                    if(newPhotos.size()==0){
532
+                        return;
533
+                    }
534
+                    photoList.addAll(newPhotos);
535
+                    photoPageAdapter.addPhotos(newPhotos);
536
+                }
537
+            }
538
+        };
539
+        fetchPhotosTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.GROUP_PHOTO_LIST_URL);
540
+    }
541
+
435 542
     private void sharePhoto() {
436 543
         sharePop = new PhotoSharePopup(this,this);
437 544
         sharePop.showPopupWindow();
@@ -474,7 +581,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
474 581
         if (checkOrderTask != null && checkOrderTask.getStatus() == AsyncTask.Status.RUNNING) {
475 582
             checkOrderTask.cancel(true);
476 583
         }
477
-        HashMap<String, String> params = new HashMap<String, String>();
584
+        HashMap<String, String> params = new HashMap<>();
478 585
         params.put("order_id", orderId);
479 586
         if(!TextUtils.isEmpty(transaction)){
480 587
             params.put("transaction_id", transaction);
@@ -524,11 +631,11 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
524 631
             wxPayTask.cancel(true);
525 632
         }
526 633
         int feeInteger =(int)(fee *100);
527
-        HashMap<String, String> params = new HashMap<String, String>();
634
+        HashMap<String, String> params = new HashMap<>();
528 635
         params.put("photo_id", currentPhotoItem.photoId);
529 636
         params.put("user_id", Preferences.getInstance(this).getUserId());
530
-//        params.put("total_fee",String.valueOf(feeInteger));
531
-        params.put("total_fee","1");
637
+        params.put("total_fee",String.valueOf(feeInteger));
638
+//        params.put("total_fee","1");
532 639
         params.put("body",getString(R.string.order_body_buy_from,currentPhotoItem.uploaderName));
533 640
         params.put("trade_type","APP");
534 641
         params.put("photo_type",type);
@@ -588,7 +695,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
588 695
         if (getPhotoOrderInfoTask != null && getPhotoOrderInfoTask.getStatus() == AsyncTask.Status.RUNNING) {
589 696
             getPhotoOrderInfoTask.cancel(true);
590 697
         }
591
-        HashMap<String, String> params = new HashMap<String, String>();
698
+        HashMap<String, String> params = new HashMap<>();
592 699
         params.put("user_id", Preferences.getInstance(this).getUserId());
593 700
         params.put("photo_id", currentPhotoItem.photoId);
594 701
         getPhotoOrderInfoTask = new HttpPostTask(this, params) {
@@ -602,11 +709,10 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
602 709
                         JSONObject data = json.getJSONObject("data");
603 710
                         JSONObject porder = data.getJSONObject("porder");
604 711
                         if(porder.has("r_photo_url")){
605
-                            String rPhotoUrl = JSONParseUtils.getJSONString(porder.getString("r_photo_url"));
606
-                            currentPhotoItem.rawPhotoUrl = rPhotoUrl;                        }
712
+                            currentPhotoItem.rawPhotoUrl = JSONParseUtils.getJSONString(porder.getString("r_photo_url"));
713
+                        }
607 714
                         if(porder.has("m_photo_url")){
608
-                            String mPhotoUrl = JSONParseUtils.getJSONString(porder.getString("m_photo_url"));
609
-                            currentPhotoItem.mediumPhotoUrl = mPhotoUrl;
715
+                            currentPhotoItem.mediumPhotoUrl = JSONParseUtils.getJSONString(porder.getString("m_photo_url"));
610 716
                         }
611 717
                         return true;
612 718
                     }
@@ -653,7 +759,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
653 759
         if (getPriceTask != null && getPriceTask.getStatus() == AsyncTask.Status.RUNNING) {
654 760
             getPriceTask.cancel(true);
655 761
         }
656
-        HashMap<String, String> params = new HashMap<String, String>();
762
+        HashMap<String, String> params = new HashMap<>();
657 763
         params.put("photo_id", currentPhotoItem.photoId);
658 764
         params.put("user_id", Preferences.getInstance(this).getUserId());
659 765
         params.put("photo_type",type);
@@ -706,7 +812,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
706 812
         if (fetchCommentListTask != null && fetchCommentListTask.getStatus() == AsyncTask.Status.RUNNING) {
707 813
             fetchCommentListTask.cancel(true);
708 814
         }
709
-        HashMap<String, String> params = new HashMap<String, String>();
815
+        HashMap<String, String> params = new HashMap<>();
710 816
         params.put("user_id", Preferences.getInstance(this).getUserId());
711 817
         params.put("group_id", currentPhotoItem.groupId);
712 818
         params.put("photo_id", currentPhotoItem.photoId);
@@ -726,7 +832,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
726 832
                         JSONArray commentArray = info.getJSONArray("comments");
727 833
                         if (commentArray != null && commentArray.length() > 0) {
728 834
                             int len = commentArray.length();
729
-                            commentsList = new ArrayList<CommentBean>();
835
+                            commentsList = new ArrayList<>();
730 836
                             for (int k = 0; k < len; k++) {
731 837
                                 JSONObject obj = commentArray.getJSONObject(k);
732 838
                                 CommentBean bean = new CommentBean();
@@ -773,7 +879,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
773 879
         if (fetchThumbUpListTask != null && fetchThumbUpListTask.getStatus() == AsyncTask.Status.RUNNING) {
774 880
             fetchThumbUpListTask.cancel(true);
775 881
         }
776
-        HashMap<String, String> params = new HashMap<String, String>();
882
+        HashMap<String, String> params = new HashMap<>();
777 883
         params.put("user_id", Preferences.getInstance(this).getUserId());
778 884
         params.put("group_id", currentPhotoItem.groupId);
779 885
         params.put("photo_id", currentPhotoItem.photoId);
@@ -790,7 +896,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
790 896
                     if (status == 200) {
791 897
                         LogHelper.d(TAG, " fetchThumbUpListTask parseResponse status ok");
792 898
                         JSONObject info = json.getJSONObject("data");
793
-                        thumbupList = new ArrayList<CommentBean>();
899
+                        thumbupList = new ArrayList<>();
794 900
                         JSONArray commentArray = info.getJSONArray("thumbups");
795 901
                         if (commentArray != null && commentArray.length() > 0) {
796 902
                             int len = commentArray.length();
@@ -858,7 +964,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
858 964
         if (submitThumbUpTask != null && submitThumbUpTask.getStatus() == AsyncTask.Status.RUNNING) {
859 965
             submitThumbUpTask.cancel(true);
860 966
         }
861
-        HashMap<String, String> params = new HashMap<String, String>();
967
+        HashMap<String, String> params = new HashMap<>();
862 968
         params.put("user_id", Preferences.getInstance(this).getUserId());
863 969
         params.put("group_id", currentPhotoItem.groupId);
864 970
         params.put("photo_id", currentPhotoItem.photoId);
@@ -910,7 +1016,7 @@ public class PhotoDetailsActivity extends BaseActivity implements View.OnClickLi
910 1016
         if (submitCommentTask != null && submitCommentTask.getStatus() == AsyncTask.Status.RUNNING) {
911 1017
             submitCommentTask.cancel(true);
912 1018
         }
913
-        HashMap<String, String> params = new HashMap<String, String>();
1019
+        HashMap<String, String> params = new HashMap<>();
914 1020
         params.put("user_id", Preferences.getInstance(this).getUserId());
915 1021
         params.put("group_id", currentPhotoItem.groupId);
916 1022
         params.put("photo_id", currentPhotoItem.photoId);

+ 6 - 0
app/src/main/java/ai/pai/client/adapter/DetailPhotoPageAdapter.java

@@ -117,4 +117,10 @@ public class DetailPhotoPageAdapter extends PagerAdapter {
117 117
     public Parcelable saveState() {
118 118
         return null;
119 119
     }
120
+
121
+    public void addPhotos(ArrayList<GroupPhotoItem> photoItems){
122
+        photoList.addAll(photoItems);
123
+        notifyDataSetChanged();
124
+    }
125
+
120 126
 }

+ 3 - 3
app/src/main/java/ai/pai/client/adapter/PhotoStaggeredAdapter.java

@@ -108,13 +108,13 @@ public class PhotoStaggeredAdapter extends RecyclerView.Adapter<PhotoStaggeredAd
108 108
             public void onClick(View v) {
109 109
                 Intent intent = new Intent(context, PhotoDetailsActivity.class);
110 110
                 ArrayList<GroupPhotoItem> groupPhotoList = new ArrayList<>();
111
-                if(photoList.size()<200){
111
+                if(photoList.size()<300){
112 112
                     groupPhotoList.addAll(photoList);
113 113
                 }else{
114 114
                     if(position>10){
115
-                        groupPhotoList.addAll(photoList.subList(position-9,position+190>photoList.size()?photoList.size():position+190));
115
+                        groupPhotoList.addAll(photoList.subList(position-9,position+290>photoList.size()?photoList.size():position+290));
116 116
                     }else{
117
-                        groupPhotoList.addAll(photoList.subList(0,200));
117
+                        groupPhotoList.addAll(photoList.subList(0,300));
118 118
                     }
119 119
                 }
120 120
                 intent.putExtra("photo_item",item);

+ 0 - 1
app/src/main/java/ai/pai/client/fragments/GroupPhotoFragment.java

@@ -1,6 +1,5 @@
1 1
 package ai.pai.client.fragments;
2 2
 
3
-import android.app.Activity;
4 3
 import android.content.ComponentName;
5 4
 import android.content.Context;
6 5
 import android.content.Intent;